package main

import (
	"sync"
	"fmt"
	"time"
)

/*func f1() {
	fmt.Println("This is f1 function")
}
func f2() {
	fmt.Println("This is f2 function")
}
func f3() {
	fmt.Println("This is f3 function")
}
func main() { //once对象的Do方法，只允许被调用一次
	var once sync.Once
	once.Do(f1)
	once.Do(f2)
	once.Do(f3)
}*/
var a string
var once sync.Once
func setup() {
	fmt.Println("setup begins.")
	a = "hello"
	for i := 1; i <= 10; i++ {
		time.Sleep(1e9)
		fmt.Print(".")
	}
	fmt.Println("\nsetup ends.")
}
func print(wg *sync.WaitGroup) {
	once.Do(setup)
	fmt.Println(a)
	wg.Done()
}
func main() {
	var wg sync.WaitGroup
	wg.Add(2)
	go print(&wg)
	go print(&wg)
	wg.Wait()
}
